package com.bb.blog.rabbitmq.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMqConfig {
    private static Logger logger = LoggerFactory.getLogger(RabbitMqConfig.class.toString());

    public static final String QUEUE_1 = "QUEUE_1";
    public static final String QUEUE_2 = "QUEUE_2";
    public static final String QUEUE_3 = "QUEUE_3";
    public static final String QUEUE_DEAD = "QUEUE_DEAD";
    public static final String QUEUE_DELAY = "QUEUE_DELAY";


    public static final String EXCHANGE_DIRECT = "DIRECT";
    public static final String EXCHANGE_FANOUT = "FANOUT";
    public static final String EXCHANGE_TOPIC = "TOPIC";

    public static final String EXCHANGE_DEAD = "DEAD";


    public RabbitMqConfig() {
        logger.info("初始化消息队列绑定策略");
    }

    @Bean
    public Queue queue1() {
        return new Queue(QUEUE_1, true);
    }

    @Bean
    public Queue queue2() {
        return new Queue(QUEUE_2, true);
    }

    @Bean
    public Queue queue3() {
        return new Queue(QUEUE_3, true);
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(EXCHANGE_DIRECT, true, true);
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(EXCHANGE_FANOUT, true, true);
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(EXCHANGE_TOPIC, true, true);
    }

    //    @Bean
//    public Binding binding(){
//        return BindingBuilder.bind(queue1()).to(directExchange()).with("debug");
//    }
    @Bean
    Binding binding2() {
        return BindingBuilder.bind(delayQueue()).to(directExchange()).with("delay");
    }

    @Bean
    public Queue deadQueue() {
        Queue queue = new Queue(QUEUE_DEAD, true);
        return queue;

    }

    @Bean
    public Queue delayQueue() {
        Queue delay = QueueBuilder.durable(QUEUE_DELAY)
                .withArgument("x-dead-letter-exchange", EXCHANGE_DEAD)
                .withArgument("x-dead-letter-routing-key", "DEAD")
                .withArgument("x-message-ttl", 1000)
                .build();
        return delay;
    }


    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange(EXCHANGE_DEAD);
    }

    @Bean
    public Binding deadBinding() {
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with("DEAD");
    }


}
